package cn.wp.ewallet.wallet.dao.impl;

import cn.wp.common.core.dao.JdbcSupportTemplate;
import cn.wp.common.core.dao.SqlString;
import cn.wp.common.core.model.WpPage;
import cn.wp.ewallet.wallet.dao.UserPublishDao;
import cn.wp.ewallet.wallet.model.entity.UserCollectDo;
import cn.wp.ewallet.wallet.model.entity.UserPublishDoo;
import io.swagger.annotations.ApiParam;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class UserPublishDaoImpl implements UserPublishDao {
    @Autowired
    private JdbcSupportTemplate jdbcSupportTemplate;

    @Override
    public WpPage pubList(Integer type, String userId, Integer pageNo, Integer pageSize) {
        SqlString sqlString = new  SqlString();
        Object[] objArr = new Object[1];
        sqlString.add("SELECT u.USER_NIKENAME as userNickName,up.* FROM user_publish up LEFT JOIN pa_user_info u ON up.USER_ID = u.USER_ID WHERE 1=1 ");
        if (null != type) {
            sqlString.add("and up.TYPE = ? and up.STATUS = true");
            objArr[0] = type;
        }else if (Strings.isNotBlank(userId)) {
            sqlString.add("and up.USER_ID = ?");
            objArr[0] = userId;
        }else if(type!=null && Strings.isNotBlank(userId)){
            sqlString.add("and up.TYPE = ? and up.STATUS = true");
            sqlString.add("and up.USER_ID = ?");
            objArr = new Object[2];
            objArr[0] = type;
            objArr[1] = userId;
        }else{
            objArr = null;
        }
        WpPage<UserPublishDoo> userPublishDooWpPage = jdbcSupportTemplate.selectList(sqlString.build(), objArr, new WpPage<>(pageNo, pageSize), UserPublishDoo.class);
        userPublishDooWpPage.getList().forEach(pubVo->{
            SqlString sql = new  SqlString();
            sql.add("SELECT * FROM user_collect WHERE USER_ID = ?");
            List<UserCollectDo> playList = jdbcSupportTemplate.selectList(sql.build(), new Object[]{pubVo.getUserId()}, UserCollectDo.class);
            pubVo.setPayWay(playList);
        });

        return userPublishDooWpPage;
    }
}
